#ifndef DBUSCALLER_HPP
#define DBUSCALLER_HPP

#include <QObject>
#include <QtDBus>
#include <thread>
struct DbusTestModelItem;
class DbusCaller : public QObject
{
    Q_OBJECT
public:
    explicit DbusCaller(QObject *parent = nullptr);
    bool start(QVector<DbusTestModelItem *> list);
    bool start(DbusTestModelItem *item,int row);
    bool loop(QVector<DbusTestModelItem *> list,int times);
    void stop();
    bool isFinished() const;
    bool isRunning() const;
    int getCallTimeoutMs() const;
    void setCallTimeoutMs(int CallTimeoutMs);


signals:
    void onItemStart(DbusTestModelItem *item,int row);
    void onResetResult(int row);
//    void onItemFinish(DbusTestModelItem *item,int row,int loops, QDBusMessage msg);
    void onItemFinish1(DbusTestModelItem *item,int row,int loops, const QVariant &ret);
    void onError(DbusTestModelItem *item,int row,QString err);
    void onStart();
    void onFinish();
private slots:
    void onCall();
     void signalCall(const QDBusMessage &msg);
    QDBusMessage callItem(DbusTestModelItem *item, int row);
    QVariant calllocalMethod(DbusTestModelItem *item,bool *ok=NULL);
private:
    QWaitCondition m_cond;
    QMutex m_mutex;
    std::thread* m_thread;
    int m_runStates;
    int m_stop;
    int m_loop;
    int m_loop_times;
    int m_loop_stop_times;
    QDBusMessage m_signalMsg;
    QVector<DbusTestModelItem *> m_list;
    int m_CallTimeoutMs;
    int m_runRow;
    int m_runStatus;
    QDBusMessage waitSignal(DbusTestModelItem *item,  QDBusConnection bus);
    QDBusMessage getProperty(DbusTestModelItem *item,  QDBusConnection bus);
    QDBusMessage setProperty(DbusTestModelItem *item,  QDBusConnection bus);
    QDBusMessage callMethod(DbusTestModelItem *item,  QDBusConnection bus);
};

#endif // DBUSCALLER_HPP
